import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.io.IOException;

import javax.swing.Timer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;


public class gameServer extends Class1{
	Server s;

	int lastID = 2;
	int cLastID = 0;
	
	boolean con = false;
	public gameServer()
	{
		super();
		rectangle = new rect(10, 1, 50, 50, 10); // x, y, width, height, speed
		rectangle.id = 0;

		rectangle2 = new rect(500, 450, 30, 30, 4);
		rectangle2.id = 1;

		t = new Timer(60, this);
		t.start();

		dataT = new Timer(60, this);



		s = new Server();

		Kryo k = s.getKryo();
		k.register(rect.class);
		k.register(missile.class);
		k.register(Rectangle2D.Double.class);
		k.register(Ellipse2D.Double.class);
		k.register(char[].class);
		k.register(circle.class);
		k.register(missile.class);

		try {
			s.bind(12345);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		final Class1 c = this;
		s.addListener(new Listener() {
			public void connected (Connection connection) {
				System.out.println("--CONNECTED TO CLIENT--");
				dataT.start();
			}

			public void disconnected(Connection connection){
				System.out.println("--DISCONNECTED FROM CLIENT--");
			}

			public void received (Connection connection, Object object) {
				if (object instanceof rect) {
					rect request = (rect)object;

					badRect.put(request.hashCode(), request);
					con = true;
					System.out.println(">>Bad Rectangle received from Client<<");

					if(!circles.containsKey(request.hashCode()))
					{
						System.out.println(">>>>CIRCLE " + cLastID + " ADDED");
						circle c = new circle(10, 10, 20, 20, 3);			

						c.setID(request.hashCode());
						c.active = true;

						c.otherRect = request;

						circles.put(request.hashCode(), c);

						cLastID++;
					}
				}
				if(object instanceof String)
				{
					String m = (String) object;
					if(m.equals("GETID"))
					{
						System.out.println(">>>>GOT ID REQUEST: ASSIGNING " + lastID);
						connection.sendTCP(Integer.toString(lastID));

						lastID++;
					}
					else if(m.contains("INGAMEMSG:"))
					{
						m = m.replace("INGAMEMSG: ", "");
						
						currentMessage = m;
					}
				}
			}
		});

		s.start();

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(dataT))
		{
			//Send message if applicable
			if(currentMessage != null && !currentMessage.equals(""))
			{
				s.sendToAllTCP("INGAMEMSG: " + currentMessage);
			}
			
			if(sWidth != frame.getWidth() || sHeight != frame.getHeight())
			{
				System.out.println(">>Window size change: Sent to all clients<<");
				s.sendToAllTCP(frame.getWidth() + "X" + frame.getHeight());
				sWidth = frame.getWidth();
				sHeight = frame.getHeight();
			}
			
			if(lost == true)
			{
				s.sendToAllTCP("WIN");
			}

			s.sendToAllTCP(rectangle);
			s.sendToAllTCP(rectangle2);

			for(rect r: badRect.values())
			{
				if(r.id == -5)
				{
					r.id = lastID;
					lastID++;
				}
				s.sendToAllTCP(r);
			}

			//Send circles to all clients
			for(circle c: circles.values())
			{
				if(c.active = true)
				{
					System.out.println(">>Circle " + c.hashCode() + " sent to server<<");
					s.sendToAllTCP(c);
				}
			}
			
			//Update missile pos + send to all clients + delete if applicable
			for(missile c: missiles.values())
			{
				s.sendToAllTCP(c);
			}
			
		}


		else if(arg0.getSource().equals(t))
		{
			for(rect r: badRect.values())
			{
				if(rectangle.rectObject.intersects(r.rectObject))
				{
					t.stop();
					lost = true;
					System.out.println("Rectangle intersected rectangle2 *LOSE*");
				}
			}

			if(rectangle.rectObject.intersects(rectangle2.rectObject))
			{
				t.stop();
				lost = true;
				System.out.println("Rectangle intersected rectangle2 *LOSE*");
			}

			if(panel != null)
			{
				rectangle.update(panel.getWidth(), panel.getHeight());

				for(rect r: badRect.values())
				{
					r.update(panel.getWidth(), panel.getHeight());
				}
				rectangle2.update(rectangle.rectObject, panel.getWidth(), panel.getHeight());
				
				for(circle c: circles.values())
				{
					if(c.active == true)
					{
						System.out.println(">>Circle " + c.hashCode() + " updated<<");
						c.otherRect = badRect.get(c.otherRect.id);
						c.update(panel.getWidth(), panel.getHeight());
					}
				}
				
				//Update all missiles
				for(missile c: missiles.values())
				{
					c.update(panel.getWidth(), panel.getHeight());
				}
			}
			

			this.repaint();	
		}
	}

}
